package com.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;
import java.util.List;

import javax.naming.NamingException;

import com.domain.Products;

public class ProductsDAO extends BaseDAO {
	public int insert(Products product) {
		int result = 0;
		try {
			Connection conn = getConnection();
			String sql = "insert into products values(?, ?, ?, ?, ?)";
			PreparedStatement stmt = conn.prepareStatement(sql);
			stmt.setString(1, product.getProductCode());
			stmt.setString(2, product.getProductName());
			stmt.setFloat(3, product.getSalesPrice());
			stmt.setFloat(4, product.getBuyPrice());
			stmt.setString(5, product.getDescription());
			result = stmt.executeUpdate();
			conn.close();
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}

	public List<Products> getAllProduct() {
		List<Products> list = new LinkedList<Products>();
		try {
			Connection conn = getConnection();
			String sql = "select * from products";
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			while (rs.next()) {
				String productCode = rs.getString("productCode");
				String productName = rs.getString("productName");
				float salesPrice = rs.getFloat("salesPrice");
				float buyPrice = rs.getFloat("buyPrice");
				String description = rs.getString("description");
				list.add(new Products(productCode, productName, salesPrice,
						buyPrice, description));
			}
			conn.close();
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

	public Products getProductById(String code) {
		Products p = null;
		try {
			Connection conn = getConnection();
			String sql = "select * from products where productcode=?";
			PreparedStatement stmt = conn.prepareStatement(sql);
			stmt.setString(1, code);
			ResultSet rs = stmt.executeQuery();
			rs.next();
			if (!rs.wasNull()) {
				String productCode = rs.getString("productCode");
				String productName = rs.getString("productName");
				float salesPrice = rs.getFloat("salesPrice");
				float buyPrice = rs.getFloat("buyPrice");
				String description = rs.getString("description");
				p = new Products(productCode, productName, salesPrice, buyPrice,
						description);
			}
			conn.close();
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return p;
	}

	public void deleteProduct(Products product) {
		try {
			Connection conn = getConnection();
			String sql = "delete from products where productcode=? and productname=? and salesprice=? and buyprice=? and description=? ";
			PreparedStatement stmt = conn.prepareStatement(sql);
			stmt.setString(1, product.getProductCode());
			stmt.setString(2, product.getProductName());
			stmt.setFloat(3, product.getSalesPrice());
			stmt.setFloat(4, product.getBuyPrice());
			stmt.setString(5, product.getDescription());
			stmt.executeUpdate();		
			conn.close();	
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
